<?php
/**
 * 模拟emarsys_new服务器的返回
 * 模拟程序使用注意点：
 * 1. 修改php.ini参数'always_populate_raw_post_data' 设为 -1。 需要这样修改的原因是提交给emarsys_new的post内容为xml格式
 * 2. 在请求页面中设置GET参数simulate为1
 * 3. 修改redis 中hash类型key为emarsys_new_simulate中的trigger字段。如果trigger为1，模拟成功。如果trigger为0，模拟失败
 * Created by PhpStorm.
 * User: Liujianlin
 * Date: 2017/6/14
 * Time: 10:25
 */

namespace app\controllers\marketing;

use yii;
use yii\web\Controller;

class EmarsysNewSimulateController extends Controller
{
    /**
     * csrf校验
     * @var bool    禁用校验
     */
    public $enableCsrfValidation = false;

    /**
     * 模拟第一步：模板返回
     */
    private function responseTemplate()
    {
        header("HTTP/1.1 200 OK");
        header("X-XSS-Protection: 1; mode=block");
        header("X-Content-Type-Options: nosniff");
    }

    /**
     * 模拟第二步：修改版返回
     */
    private function responseRevision()
    {
        header("Content-Type: application/xml");
        $date = \yii::$app->bjTime->date("Y-m-d");
        $time = \yii::$app->bjTime->date("H:i:s");
        $response = "<?xml version='1.0' encoding='UTF-8'?>\r\n<revision id=\"3\" created=\"{$date}T{$time}+0200\"/>";
        echo $response;
    }

    /**
     * 模拟第三步：触发邮件，发送成功
     */
    private function responseTriggerOK()
    {
        echo '"line","recipient_id","error"\r\n"2","435231924286390277",""';
    }

    /**
     * 模拟第三步：触发邮件，发送失败
     */
    private function responseTriggerFail()
    {
        header('HTTP/1.1 404 Not Found');
        header("status: 404 Not Found");
        echo "emarsys_new模拟程序返回失败";
    }

    /**
     * 模拟程序入口
     */
    public function actionIndex()
    {
        header("Content-type:text/html;charset=utf-8");
        $redis = new \app\components\redis\RedisMarketingEmail();
        $query = $_GET["query"];

        $arr = explode("/", $query);
        $count = count($arr);
        if ($count < 3) {
            echo "参数个数不对";
        }
        $batchName = $arr[2];

        /*
        var_dump($arr);
        exit();
        */

        if (3 == $count) {  // 第一步：模拟接收模板的返回

            // 获取休眠秒数
            $sleepTime1 = $redis->hget('emarsys_new_simulate', 'sleep_time1');
            if (!empty($sleepTime1)) {
                sleep($sleepTime1);
            }

            $this->responseTemplate();
            return ;
        }
        if (4 == $count) {  // 第二步：模拟版本返回

            // 获取休眠秒数
            $sleepTime2 = $redis->hget('emarsys_new_simulate', 'sleep_time2');
            if (!empty($sleepTime2)) {
                sleep($sleepTime2);
            }

            $this->responseRevision();
            return ;
        }
        if ($count >= 5) {  // 第三步：模拟版本返回

            // 获取休眠秒数
            $sleepTime3 = $redis->hget('emarsys_new_simulate', 'sleep_time3');
            if (!empty($sleepTime3)) {
                sleep($sleepTime3);
            }

            $trigger = $redis->hget("emarsys_new_simulate", "trigger");
            if (0 == $trigger) {
                $this->responseTriggerFail();
            } else {
                $this->responseTriggerOK();
            }
            return ;
        }

    }
}



